package com.cq.hd.app.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cq.hd.common.enums.biz.BankPayTradeTypeEnum;
import com.cq.hd.common.enums.biz.PayAttachEnum;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.*;
import com.cq.hd.order.api.HfOrderCallbackApi;
import com.cq.hd.system.api.HfApi;
import com.cq.hd.system.api.dto.HfVerifyDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class HfCallbackBiz {

    @Autowired
    private HfApi hfApi;

    @Autowired
    private HfMerchantCallbackApi hfMerchantCallbackApi;

    @Autowired
    private HfOrderCallbackApi hfOrderCallbackApi;

    @Autowired
    private TbMemberCardApi memberCardApi;

    @Autowired
    private TbMemberFeeConfigApi memberFeeConfigApi;

    @Autowired
    private TbBankPayRecordApi bankPayRecordApi;

    @Autowired
    private TbAgentEnterApplyApi agentEnterApplyApi;

    public String merchantEntAuditCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantEntAuditCallback-data:{}", data);
        log.info("merchantEntAuditCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
            // 业务处理成功
//                log.info("处理成功");

            hfMerchantCallbackApi.merchantEntAuditCallback(dataObj).unpack();
//            } else {
            // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("企业商户基本信息入驻审核消息异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String merchantOpenAuditCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantOpenAuditCallback-data:{}", data);
        log.info("merchantOpenAuditCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
            // 业务处理成功
//            log.info("处理成功");

            hfMerchantCallbackApi.merchantOpenAuditCallback(dataObj).unpack();
//            } else {
            // 业务处理失败
//                log.info("处理失败");


//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("商户业务开通审核消息异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String merchantOpenBusinessCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantOpenBusinessCallback-data:{}", data);
        log.info("merchantOpenBusinessCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
//                // 业务处理成功
//                log.info("处理成功");
//
            hfMerchantCallbackApi.merchantOpenBusinessCallback(dataObj).unpack();
//            } else {
//                // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("商户业务开通业务开通结果消息异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String merchantSettleConfigAuditCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantSettleConfigAuditCallback-data:{}", data);
        log.info("merchantSettleConfigAuditCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
//                // 业务处理成功
//                log.info("处理成功");
//
            hfMerchantCallbackApi.merchantSettleConfigAuditCallback(dataObj).unpack();
//            } else {
//                // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("子账号开通异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String wxBusinessConfigCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("wxBusinessConfigCallback-data:{}", data);
        log.info("wxBusinessConfigCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
            if ("00000000".equals(subRespCode)) {
                // 业务处理成功
                log.info("处理成功");

                hfMerchantCallbackApi.wxBusinessConfigCallback(dataObj).unpack();
            } else {
                // 业务处理失败
                log.info("处理失败");
            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("微信商户配置异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String jsPayCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("jsPayCallback-data:{}", data);
        log.info("jsPayCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String reqSeqId = dataObj.getString("req_seq_id");

            JSONObject wxResponse = dataObj.getJSONObject("wx_response");
            String attach = wxResponse.getString("attach");
            if (PayAttachEnum.ORDER_PAY.getName().equals(attach)) {
                // 订单支付处理
                log.info("订单支付聚合正扫交易异步回调");
                hfOrderCallbackApi.jsPayCallback(dataObj).unpack();
            } else if (PayAttachEnum.MEMBER_FEE_PAY.getName().equals(attach)) {
                // 开通会员支付处理
                log.info("开通会员聚合正扫交易异步回调");
                memberFeeConfigApi.jsPayCallback(dataObj).unpack();
            } else if (PayAttachEnum.MEMBER_CARD_PAY.getName().equals(attach)) {
                // 购买次卡支付处理
                log.info("购买次卡聚合正扫交易异步回调");
                memberCardApi.jsPayCallback(dataObj).unpack();
            }
//            else if (PayAttachEnum.RECHARGE_SUBSIDY_PAY.getName().equals(attach)) {
//                //  充值补贴金处理,充值补贴金只支持网银
//                log.info("充值补贴金聚合正扫交易支付成功异步回调");
//            }

            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("聚合正扫异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String bankPayCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("bankPayCallback-data:{}", data);
        log.info("bankPayCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String reqSeqId = dataObj.getString("req_seq_id");
            String remark = dataObj.getString("remark");
            if (!StringUtils.isBlank(remark)) {
                if (remark.equals(String.valueOf(BankPayTradeTypeEnum.RECHARGE_SUBSIDY.getValue()))) {
                    // 补贴金充值
                    log.info("补贴金充值网银支付异步回调");
                    bankPayRecordApi.bankPayCallback(dataObj).unpack();
                } else if (remark.equals(String.valueOf(BankPayTradeTypeEnum.MERCHANT_FEE.getValue()))) {
                    // 开通会员
                    log.info("开通会员网银支付异步回调");
                    memberFeeConfigApi.bankPayCallback(dataObj).unpack();
                }
                if (remark.equals(String.valueOf(BankPayTradeTypeEnum.MERCHANT_CARD.getValue()))) {
                    // 购买次卡
                    log.info("购买次卡网银支付异步回调");
                    memberCardApi.bankPayCallback(dataObj).unpack();
                }
            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("网银支付异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String merchantEntIndvAuditCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantEntIndvAuditCallback-data:{}", data);
        log.info("merchantEntIndvAuditCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
            if ("00000000".equals(subRespCode)) {
                // 业务处理成功
                log.info("处理成功");

                hfMerchantCallbackApi.merchantEntIndvAuditCallback(dataObj).unpack();
            } else {
                // 业务处理失败
                log.info("处理失败");
            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("个人商户基本信息入驻审核消息异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String preOrderCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("preOrderCallback-data:{}", data);
        log.info("preOrderCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
            if ("00000000".equals(subRespCode)) {
                // 业务处理成功
                log.info("处理成功");

                hfOrderCallbackApi.preOrderCallback(dataObj).unpack();
            } else {
                // 业务处理失败
                log.info("处理失败");
            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("微信小程序预下单异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String refundCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("refundCallback-data:{}", data);
        log.info("refundCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
            if ("00000000".equals(subRespCode)) {
                // 业务处理成功
                log.info("处理成功");

                hfOrderCallbackApi.refundCallback(dataObj).unpack();
            } else {
                // 业务处理失败
                log.info("处理失败");
            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("交易退款异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String agentUserEntAuditCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("agentUserEntAuditCallback-data:{}", data);
        log.info("agentUserEntAuditCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
//            if (!hfApi.verify(hfVerifyDto).unpack()) {
//                throw new RuntimeException("验签异常");
//            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
            if ("00000000".equals(subRespCode)) {
                // 业务处理成功
                log.info("处理成功");

                agentEnterApplyApi.agentUserEntAuditCallback(dataObj).unpack();
            } else {
                // 业务处理失败
                log.info("处理失败");
            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("用户业务入驻异步通知回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String agentUserSignCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("agentUserSignCallback-data:{}", data);
        log.info("agentUserSignCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
//            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
            // 业务处理成功
            log.info("处理成功");

            agentEnterApplyApi.agentUserSignCallback(dataObj).unpack();
//            } else {
            // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("用户签约结果异步通知回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String merchantEntUpdateCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantEntUpdateCallback-data:{}", data);
        log.info("merchantEntUpdateCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
            // 业务处理成功
//                log.info("处理成功");

//            hfMerchantCallbackApi.merchantEntAuditCallback(dataObj).unpack();
//            } else {
            // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("商户基本信息修改异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String agentUserEntModifyCallback(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("agentUserEntModifyCallback-data:{}", data);
        log.info("agentUserEntModifyCallback-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
            // 业务处理成功
//                log.info("处理成功");

//            hfMerchantCallbackApi.merchantEntAuditCallback(dataObj).unpack();
//            } else {
            // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("用户业务入驻修改异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }

    public String merchantSettleModifyAudit(HfVerifyDto hfVerifyDto) {
        String data = hfVerifyDto.getData();
        String sign = hfVerifyDto.getSign();
        log.info("merchantSettleModifyAudit-data:{}", data);
        log.info("merchantSettleModifyAudit-sign:{}", sign);

        try {
            // 使用汇付公钥验签
            if (!hfApi.verify(hfVerifyDto).unpack()) {
                throw new RuntimeException("验签异常");
            }

            JSONObject dataObj = JSON.parseObject(data);
            String subRespCode = dataObj.getString("sub_resp_code");
            String reqSeqId = dataObj.getString("req_seq_id");
//            if ("00000000".equals(subRespCode)) {
//                // 业务处理成功
//                log.info("处理成功");
//
//            hfMerchantCallbackApi.merchantSettleModifyAuditCallback(dataObj).unpack();
//            } else {
//                // 业务处理失败
//                log.info("处理失败");
//            }
            return "RECV_ORD_ID_" + reqSeqId;
        } catch (Exception e) {
            log.error("修改子账号配置异步回调异常，参数，request={}", data, e);
            Throw.isBusinessException("处理失败");
        }
        return "";
    }
}
